package com.liuyi.tools.web.resp;

import cn.hutool.core.util.StrUtil;
import com.liuyi.tools.web.unusual.BizEnum;
import com.liuyi.tools.web.unusual.IEnum;

import java.text.MessageFormat;

/**
 * @version 1.0
 * @Author MR.fmy
 * @Description Response 返回调用类
 * @Date 2019/5/15 0015 09:05
 **/
public class ResponseCall {

    public static <T> Response<T> successMsg(String msg) {
        return success(msg, null, null);
    }

    public static <T> Response<T> successMsg(String msg, Object[] args) {
        return success(msg, null, args);
    }

    public static <T> Response<T> success(T data) {
        return success("OK", data);
    }


    public static <T> Response<T> success(String msg, T data) {
        return success(msg, data, null);
    }

    /**
     * 正确返回
     *
     * @param msg  提示信息 或 提示信息key值
     * @param data 数据
     * @param args 格式化参数
     * @param <T>
     * @return {@linkplain Response}
     */
    public static <T> Response<T> success(String msg, T data, Object[] args) {
        msg = StrUtil.isBlank(msg) ? "OK" : msg;

        return new Response<T>(BizEnum.OK, StrUtil.format(msg, args), data);
    }

    public static <T> Response<T> page(T data, Object count, Integer pageIndex, Integer pageSize) {
        return page(data, count, pageIndex, pageSize, null);
    }

    public static <T> Response<T> page(T data, Object count, Integer pageIndex, Integer pageSize, Object[] args) {
        return page(null, data, count, pageIndex, pageSize, args);
    }

    /**
     * 分页返回
     *
     * @param msg   提示信息 或 提示信息key值
     * @param data  数据
     * @param count 条数
     * @param args  格式化参数
     * @param <T>
     * @return {@linkplain Response}
     */
    public static <T> Response<T> page(String msg, T data, Object count, Integer pageIndex, Integer pageSize, Object[] args) {
        msg = StrUtil.isNotBlank(msg) ? msg : "OK";
        count = count != null ? count : 0;
        if (count instanceof Long || count instanceof Integer) {
            return new Response<T>(BizEnum.OK, StrUtil.format(msg, args), data, Integer.valueOf(count.toString()), pageIndex, pageSize);
        } else {
            throw new ClassCastException("Count must be Number type");
        }
    }

    public static <T> Response<T> error(IEnum iEnum) {
        return error(iEnum, iEnum.getDefultMsg());
    }

    public static <T> Response<T> error(String errMsg) {
        return error(BizEnum.BUSINESS_FAIL, errMsg);
    }

    public static <T> Response<T> error(String errMsg, Object[] args) {
        return error(BizEnum.BUSINESS_FAIL, errMsg, args);
    }

    public static <T> Response<T> error(String code, String errMsg) {
        return error(code, errMsg, null);
    }

    public static <T> Response<T> error(IEnum iEnum, String errMsg) {
        return error(iEnum, errMsg, null);
    }

    public static <T> Response<T> error(IEnum iEnum, String errMsg, Object[] args) {
        errMsg = StrUtil.isBlank(errMsg) ? iEnum.getDefultMsg() : errMsg;
        return error(iEnum.getCode(), errMsg, args);
    }

    /**
     * 失败返回
     *
     * @param code   错误码
     * @param errMsg 失败提示信息 或 提示信息key
     * @param args   格式化参数
     * @return {@linkplain Response}
     */
    public static <T> Response<T> error(String code, String errMsg, Object[] args) {
        return new Response<T>().setCode(code).setErrMsg(StrUtil.format(errMsg, args));
    }
}
